import { BasicColumn } from "/@/components/Table"
import { FormSchema } from "/@/components/Table"
import { rules } from "/@/utils/helper/validator"
import { render } from "/@/utils/common/renderUtils"
import { UploadBizType } from "/@/enums/uploadEnum"
import { DescItem } from "/@/components/Description/index"
import { h } from 'vue'
import Copy from "/@/components/My/Copy.vue"
export const TAB_TYPE = {
  BASE_CONFIG: "baseConfig",
  PROTOCOL_CONFIG: "protocolConfig",
  ACCESS_AUTHORIZATION: "accessAuthorization",
  LOGIN_CONTROLL: "loginControll",
  SECRET_CONFIG: "secretConfig",
}

export enum AppStatusEnum {
  OFFLINE = 1,
  DEPLOY = 2,
}

export enum AppTypeEnum {
  // 自建应用
  CUSTOM = 1,
  // 官方应用
  PLATFORM = 2,
  // 生态应用
  ECOLOGY = 3
}

export enum PageUrl {
  APP_LIST = "/console/microApp",
}

export const columns: BasicColumn[] = [
  {
    title: "应用ID",
    dataIndex: "appId",
    width: 150,
  },
  {
    title: "图标",
    dataIndex: "logo",
    width: 80,
    customRender: ({ text }) => {
      return render.renderAvatar(text || "/@/assets/images/default-app.png")
    },
  },

  {
    title: "应用名",
    dataIndex: "name",
    width: 150,
  },
  {
    title: "应用类型",
    dataIndex: "appTypeName",
    width: 100,
    customRender: ({ text }) => {
      return text ? text : '-'
    },
  },
  {
    title: "说明",
    dataIndex: "remark",
    width: 150,
    customRender: ({ text }) => {
      return text ? text : '-'
    },
  },
  {
    title: "安装时间",
    dataIndex: "createdAt",
    width: 150,
  },
  {
    title: "状态",
    dataIndex: "tenantAppStatus",
    customRender: ({ text }) => {
      let color = "green"
      if (text == 2) {
        color = "red"
      }
      return render.renderTag(render.renderDict(text, "DataAvailable"), color)
    },
    width: 60,
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    label: "名称",
    field: "name",
    component: "Input",
  },
  {
    label: "状态",
    field: "status",
    component: "JDictSelectTag",
    componentProps: {
      allowClear: true,
      dictType: "DataAvailable",
      placeholder: "全部",
      stringToNumber: true,
    },
  },
  {
    label: "应用类型",
    field: "status",
    component: "JDictSelectTag",
    componentProps: {
      allowClear: true,
      dictType: "TenantAppType",
      placeholder: "全部",
      stringToNumber: true,
    },
  },
]

export const formSchema: FormSchema[] = [
  {
    label: "",
    field: "divider",
    component: "Input",
    render: () => {
      return render.renderDividerWithValue("基础信息", { orientation: 'left' })
    },
    span: 2,
  },
  {
    label: "",
    field: "id",
    component: "Input",
    show: false,
  },
  {
    label: "应用名称",
    field: "name",
    required: true,
    component: "Input",
  },
  {
    label: "应用标识",
    field: "code",
    helpMessage: '应用二级域名必须是这个标识',
    required: true,
    component: "Input",
  },

  {
    label: "应用图标",
    field: "logo",
    component: "MyImageUpload",
    componentProps: {
      fileMax: 1,
      bizType: UploadBizType.GENERAL_IMAGE,
    },
  },
  {
    field: "remark",
    label: "备注",
    component: "InputTextArea",
    componentProps: {
      autoSize: {
        minRows: 3,
        maxRows: 8,
      },
    },
  },
  {
    label: "",
    field: "drivider",
    component: "Input",
    render: () => {
      return render.renderDividerWithValue("认证配置", { orientation: 'left' })
    },
    span: 2,
  },

  // {
  //   label: "自定义登录配置",
  //   field: "customLoginConfigFlag",
  //   helpMessage: '开启后, 您可以自定义应用的登录配置项目, 实现品牌化',
  //   required: true,
  //   component: "RadioGroup",
  //   defaultValue: false,
  //   componentProps: {
  //     options: [
  //       {
  //         label: "是",
  //         value: true,
  //       },
  //       {
  //         label: "否",
  //         value: false,
  //       },
  //     ],
  //   },
  // },
  {
    label: "登录回调地址",
    field: "loginRedirectUrls",
    required: true,
    component: "MyAddSingleInput",
    componentProps: {
      placeholder: "请输入登录成功回调地址"
    },
    helpMessage: 'Redirect URI 白名单，应用在请求登录时携带 redirect_uri 参数，该值需要在白名单中，IAM 才会在认证完成后发起跳转。若有多条，请点击添加进行扩展',
    // itemProps: {
    //   helpPosition: 'item'
    // },
  },

  {
    label: "应用首页地址",
    field: "homePageUrl",
    helpMessage: '点击应用图标时，会跳转到此 URL，默认为 IAM 登录页。',
    required: false,
    component: "Input",
    // itemProps: {
    //   helpPosition: 'item'
    // },
    componentProps: {
      placeholder: "请输入应用首页链接"
    },
  },
  {
    label: " ",
    itemProps: {
      colon: false
    },
    field: "footer-action",
    component: "Input",
    slot: 'footer-action'
  },
]



export const loginControllFormSchema: FormSchema[] = [
  {
    label: "",
    field: "divider1",
    component: "Input",
    render: () => {
      return render.renderDividerWithValue("登录注册信息补全",
        {
          orientation: 'left', dashed: true,
          style: { marginBottom: '-5px', marginTop: '-5px', marginLeft: '80px' }
        }
      )
    },
    span: 2,
  },
  {
    label: "",
    field: "id",
    component: "Input",
    show: false,
  },

  {
    label: "信息补全开启位置",
    field: "completeFieldPlaces",
    helpMessage: '开启后，用户在登录/注册之后会被要求补全信息',
    required: false,
    component: "CheckboxGroup",
    defaultValue: [],
    componentProps: {
      options: [
        {
          label: "注册时信息补全",
          value: 'register',
        },
        {
          label: "登录时信息补全",
          value: 'login',
        },
      ],
    },
  },

  {
    label: "补全字段",
    field: "completeFieldNameStr",
    required: false,
    component: "Select",
    helpMessage: '当登录经过检测需要用户修改密码时候，将使用选择的第一个作为接收验证码',
    defaultValue: false,
    componentProps: {
      showSearch: true,
      optionFilterProp: "label",
      mode: "multiple",
      options: [
        {
          label: "手机号",
          value: 'phone',
        },
        {
          label: "邮箱",
          value: 'email',
        },
      ],
    },
  },

  {
    label: " ",
    itemProps: {
      colon: false
    },
    field: "footer-action",
    component: "Input",
    slot: 'footer-action'
  },
]

/**
 * 协议配置
 */
export const protocolFormSchema: FormSchema[] = [
  {
    label: "",
    field: "divider-1",
    component: "Input",
    render: () => {
      return render.renderDividerWithValue("基础配置", { orientation: 'left' })
    },
    span: 2,
  },
  {
    label: "",
    field: "id",
    component: "Input",
    show: false,
  },
  {
    label: "默认协议",
    field: "protocol",
    required: true,
    component: "JDictSelectTag",
    helpMessage: '当直接访问认证地址时默认使用的协议。禁用的协议需要在下方「授权配置」中开启并保存后方可启用。',
    defaultValue: "oidc",
    itemProps: {
      colon: true
    },
    componentProps: {
      type: "radio",
      dictType: "AuthProtocolType",
    },
  },

  {
    label: "",
    field: "divider-2",
    component: "Input",
    render: () => {
      return render.renderDividerWithValue("授权配置", { orientation: 'left' })
    },
    span: 2,
  },

  {
    label: "授权模式",
    field: "oidcConfig_grantTypes",
    required: true,
    component: "JDictSelectTag",
    helpMessage: '如果开启 implicit，请确保回调地址全部为 https',
    defaultValue: [],
    componentProps: {
      type: "select",
      mode: "multiple",
      dictType: "GrantTypes",
    },
  },
  {
    label: "返回类型",
    field: 'oidcConfig_responseTypes',
    required: true,
    defaultValue: ['code'],
    component: "JDictSelectTag",
    helpMessage: '配置 Iam 支持从授权端点返回的数据项，code 代表支持返回授权码，token 代表支持返回 Access token，id_token 代表支持返回 Id token，但如果发起请求时不主动向 Iam 索取，Iam 不会主动返回相应数据',
    componentProps: {
      type: "select",
      mode: "multiple",
      dictType: "AuthResourceType",
    },
  },
  {
    label: "授权码过期时间",
    field: "authorizationCodeExpire",
    required: true,
    slot: 'authorizationCodeExpire',
    component: "Input",
    helpMessage: '兑换 token 的授权码过期时间（建议 600 秒），若在有效期内未使用，则导致所有身份或资源凭证无法兑换，最终无法进行登录。',
    defaultValue: "600"
  },
  {
    label: "id_token 过期时间",
    field: "idTokenExpire",
    required: true,
    slot: 'idTokenExpire',
    component: "Input",
    helpMessage: 'Id_token 是指用户的身份凭证（建议 1209600 秒/ 14 天），过期后用户的身份信息无法认证，需要重新登录，若需要延长登录持续时间，可以修改该配置',
    defaultValue: "600"
  },
  {
    label: "access_token 过期时间",
    field: "accessTokenExpire",
    required: true,
    slot: 'accessTokenExpire',
    component: "Input",
    helpMessage: 'access_token 是允许访问资源的凭证（建议 1209600 秒/ 14 天），过期后用户的权限资源无法认证，需要重新登录',
    defaultValue: "600"
  },
  {
    label: "refresh_token 过期时间",
    field: "refreshTokenExpire",
    slot: 'refreshTokenExpire',
    required: true,
    component: "Input",
    helpMessage: 'refresh_token 是获取新的 access_token、id_token 的凭证（建议 2592000 秒/ 30 天，该配置一般比 access_token 要长），过期后无法刷新 id_token 和 access_token，最终会导致用户需要重新登录',
    defaultValue: "600"
  },
  {
    label: "",
    field: "divider-3",
    component: "Input",
    render: () => {
      return render.renderDividerWithValue("配置信息", { orientation: 'left' })
    },
    span: 2,
  },

  {
    label: " ",
    itemProps: {
      colon: false
    },
    field: "footer-action",
    component: "Input",
    slot: 'footer-action'
  },
]



export const newAppFormSchema: FormSchema[] = [
  {
    label: "",
    field: "id",
    show: false,
    component: "Input",
  },
  {
    label: "应用名称",
    field: "name",
    required: true,
    component: "Input"
  },
  {
    label: "应用分组",
    field: "groupId",
    required: false,
    component: "Input"
  },
  {
    field: "remark",
    label: "备注",
    component: "InputTextArea",
    componentProps: {
      autoSize: {
        minRows: 3,
        maxRows: 8,
      },
    },
  },

]


export const baseInfoDescSchema: DescItem[] = [
  {
    field: "id",
    label: "应用ID",
    span: 2,
    render: (curVal, data) => {
      return h(Copy, { value: curVal })
    },
  },
  {
    field: "createdAt",
    label: "创建时间",
    span: 2,
  },
  {
    field: "mainAppId",
    label: "场景",
    span: 2,
    render: (curVal, data) => {
      let color = "cyan"
      if (curVal == data.id) {
        color = "blue"
      }
      return render.renderTag(curVal == data.id ? '管理端' : '业务端', color)
    },
  },
  {
    field: "status",
    label: "状态",
    span: 2,
    render: (curVal, data) => {
      return render.renderDict(curVal, "use_status")
    },
  },
]


/**
 * 密钥信息
 */
export const secretFormSchema: FormSchema[] = [
  {
    field: "id",
    label: "id",
    component: "Input",
    show: false,
  },
  {
    field: "asymmetryCryptoType",
    label: "非对称加密类型",
    defaultValue: "RSA",
    helpMessage: [
      "对称加密公钥, 比如前端在js中存放公钥(混淆后)",
      "页面在启动时候会根据appId获取aes密钥key和签名key(经过私钥加密)",
      "然后部分接口就可以使用aes加密传输, 且效率比rsa加密快, 前端签名key使用signKey",
    ],
    component: "JDictSelectTag",
    componentProps: {
      dictType: "asymmetryCryptoType",
      type: "radio",
    },
  },
  {
    field: "publicKey",
    label: "公钥Key",
    component: "InputTextArea",
    required: true,
    ifShow: ({ values }) => {
      return !!values.publicKey
    },
    componentProps: {
      autoSize: {
        minRows: 6,
        maxRows: 6,
      },
      readOnly: true,
    },
  },

  {
    field: "privateKey",
    label: "私钥Key",
    helpMessage: "提供给调用方, 一般用于加密获取密钥数据(签名Key, Aes加密Key等)",
    component: "InputTextArea",
    required: false,
    ifShow: ({ values }) => {
      return !!values.privateKey
    },
    componentProps: {
      autoSize: {
        minRows: 6,
        maxRows: 6,
      },
      readOnly: true,
    },
  },

  {
    field: "symmetryCryptoType",
    label: "对称加密类型",
    defaultValue: "AES",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "symmetryCryptoType",
      type: "radio",
    },
  },

  {
    field: "secretKey",
    label: "加密密钥Key",
    component: "InputTextArea",
    required: false,
    ifShow: ({ values }) => {
      return !!values.secretKey
    },
    componentProps: {
      autoSize: {
        minRows: 3,
        maxRows: 3,
      },
      readOnly: true,
    },
  },

  {
    field: "signKey",
    label: "签名Key",
    component: "InputTextArea",
    required: false,
    ifShow: ({ values }) => {
      return !!values.signKey
    },
    componentProps: {
      autoSize: {
        minRows: 3,
        maxRows: 3,
      },
      readOnly: true,
    },
  },
  {
    label: " ",
    itemProps: {
      colon: false
    },
    field: "footer-action",
    component: "Input",
    slot: 'footer-action'
  },
]
